package com.stn.zjx.givingtreasure.activity.search;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.came.viewbguilib.ButtonBgUi;
import com.google.android.flexbox.FlexboxLayout;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.StoreShopSearchItemListAdapter;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.event.StoreShopSearchEvent;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;
import com.vondear.rxtools.RxActivityTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StoreShopSearchActivity extends BaseActivity {


    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.skip_bt)
    TextView skipBt;
    @BindView(R.id.share_bt)
    RelativeLayout shareBt;
    @BindView(R.id.more_bt)
    RelativeLayout moreBt;
    @BindView(R.id.scan_bt)
    RelativeLayout scanBt;
    @BindView(R.id.release_bt)
    ButtonBgUi releaseBt;
    @BindView(R.id.search_iv)
    ImageView searchIv;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.search_bg)
    RelativeLayout searchBg;
    @BindView(R.id.save_bt)
    TextView saveBt;
    @BindView(R.id.finish_bt)
    TextView finishBt;
    @BindView(R.id.search_tv_bt)
    TextView searchTvBt;
    @BindView(R.id.robot_open_bt)
    RelativeLayout robotOpenBt;
    @BindView(R.id.clear_search_content_bt)
    RelativeLayout clearSearchContentBt;
    @BindView(R.id.search_store_edit)
    EditText searchStoreEdit;
    @BindView(R.id.search_store_edit_bg)
    LinearLayout searchStoreEditBg;
    @BindView(R.id.search_store_bg)
    RelativeLayout searchStoreBg;
    @BindView(R.id.clear_history_bt)
    RelativeLayout clearHistoryBt;
    @BindView(R.id.search_history)
    FlexboxLayout searchHistory;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.search_hot)
    FlexboxLayout searchHot;
    @BindView(R.id.rv)
    RecyclerView rv;
   LinearLayoutManager lm;
   StoreShopSearchItemListAdapter adapter=null;
     boolean isManual=true;
    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }
   @Subscribe(threadMode = ThreadMode.MAIN)
   public void onSearch(StoreShopSearchEvent event){
         switch (event.flag){
             case StoreShopSearchEvent.Search_Checked:
                 isManual=false;
                 adapter.clearList();
                 rv.setVisibility(View.GONE);
                 searchStoreEdit.setText(event.data);
                 searchStoreEdit.setSelection(searchStoreEdit.getText().length());
                 isManual=true;
                 break;
         }
   }
    @Override
    public void initComponent() {
        lm=new LinearLayoutManager(this);
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(lm);
        rv.setAdapter(adapter=new StoreShopSearchItemListAdapter(this));
        searchStoreBg.setVisibility(View.VISIBLE);
        searchTvBt.setVisibility(View.VISIBLE);
         searchStoreEditBg.setVisibility(View.VISIBLE);
        searchStoreEdit.setVisibility(View.VISIBLE);
        searchStoreBg.setVisibility(View.VISIBLE);
        searchStoreEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                  if(!isManual){
                      return;
                  }
                if (searchStoreEdit.getText().toString().trim().length() == 0) {
                    clearSearchContentBt.setVisibility(View.GONE);
                    robotOpenBt.setVisibility(View.VISIBLE);
                    rv.setVisibility(View.GONE);
                    adapter.clearList();
                } else {
                    robotOpenBt.setVisibility(View.GONE);
                    clearSearchContentBt.setVisibility(View.VISIBLE);
                    rv.setVisibility(View.VISIBLE);
                    update(searchStoreEdit.getText().toString().trim());
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        searchStoreEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if(TextUtils.isEmpty(searchStoreEdit.getText().toString().trim())){
                        showToast(getString(R.string.search_content_is_not_null));
                        return false;
                    }
                     Bundle bundle=new Bundle();
                    bundle.putString("data",searchStoreEdit.getText().toString().trim());
                    RxActivityTool.skipActivity(getApplicationContext(),StoreShopSearchResultActivity.class,bundle);
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_store_shop);
        ButterKnife.bind(this);
        initComponent();
        initData();
    }

    public void initData() {
        searchHistory.removeAllViews();
        List<String> ar = new ArrayList<>();
        ar.add("送宝");
        ar.add("手机");
        ar.add("笔记本电脑");
        ar.add("零食");
        ar.add("衣服");
        ar.add("化妆品");
        if (ar.size() > 0) {
            noDataTv.setVisibility(View.GONE);
        }
        for (int i = 0; i < ar.size(); i++) {
            String str = ar.get(i);
            TextView tv = (TextView) View.inflate(this, R.layout.tv_search_tag, null);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = ToolsUtils.dip2px(this, 10);
            lp.topMargin = ToolsUtils.dip2px(this, 10);
            tv.setLayoutParams(lp);
            tv.setText(str);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                     Bundle bundle=new Bundle();
                     bundle.putString("data",str);
                    RxActivityTool.skipActivity(getApplicationContext(),StoreShopSearchResultActivity.class,bundle);
                }
            });
            searchHistory.addView(tv);
        }


        searchHot.removeAllViews();
        List<String> ars = new ArrayList<>();
        ars.add("Cos");
        ars.add("牛皮");
        ars.add("方便面");
        ars.add("文具盒");


        for (int i = 0; i < ars.size(); i++) {
            String str = ars.get(i);
            TextView tv = (TextView) View.inflate(this, R.layout.tv_search_tag, null);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = ToolsUtils.dip2px(this, 10);
            lp.topMargin = ToolsUtils.dip2px(this, 10);
            tv.setLayoutParams(lp);
            tv.setText(str);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle=new Bundle();
                    bundle.putString("data",str);
                    RxActivityTool.skipActivity(getApplicationContext(),StoreShopSearchResultActivity.class,bundle);
                }
            });
            searchHot.addView(tv);
        }

    }

    @OnClick({R.id.back_bt, R.id.robot_open_bt,
            R.id.clear_search_content_bt, R.id.clear_history_bt,
            R.id.search_tv_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;
            case R.id.robot_open_bt:

                break;
            case R.id.clear_search_content_bt:
                searchStoreEdit.getText().clear();
                break;
            case R.id.clear_history_bt:
                showClear();
                break;
            case R.id.search_tv_bt:
                if(TextUtils.isEmpty(searchStoreEdit.getText().toString().trim())){
                    showToast(getString(R.string.search_content_is_not_null));
                 return;
                }
                Bundle bundle=new Bundle();
                bundle.putString("data",searchStoreEdit.getText().toString().trim());
                RxActivityTool.skipActivity(getApplicationContext(),StoreShopSearchResultActivity.class,bundle);
                break;

        }
    }

    public void showClear() {
        Dialog dialog = new Dialog(this);
        //  Objects.requireNonNull(dialog.getWindow()).setWindowAnimations(R.style.myDialogStyle_1);
        dialog.setContentView(R.layout.dialog_clear_store_shop_search_history);
        View cancel_bt = dialog.findViewById(R.id.cancel_bt);
        View sure_bt = dialog.findViewById(R.id.sure_bt);
        cancel_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        sure_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                searchHistory.removeAllViews();
            }
        });
        dialog.show();

    }

    public void update(String keyword){
             List<String> ar=new ArrayList<>();
              ar.add("衣服");
        ar.add("cos");
        ar.add("家具");
        ar.add("早餐");
        ar.add("面包");
        ar.add("披萨");
        ar.add("一堆的");
        ar.add("笔记本电脑");
        adapter.refreshList(ar);
    }

}
